import type { App as Vue } from 'vue';
import type { WebConfig } from './configs/webConfig';
import type { resolveResult} from '@/utils/PromiseHelper';
import { accessToken, currentUser, CurrentUser } from './configs/useCurrentUser';
import { sha256, sha1 } from '@/utils/sha256';
import webConfig from './configs/webConfig';
import withResolvers from '@/utils/PromiseHelper';

// 所有模块
interface App {
    web: WebConfig,
    vue?: Vue<any>,
    sha1(message: string): string,
    sha256(message: string): string,
    delay(ms: number): Promise<any>,
    GUID_EMPTY: string,
    baseDepart: string,
    toText(dict: string, key: string): string,
    accessToken: { requestKey: string, token: string },
    currentUser: CurrentUser,
    succeed(permissionOrMenu: string): Boolean,
    setTitle (title : string): void,
    withResolvers<T> () : resolveResult<T>,
    isDevelopment () : Boolean,
	redirectTo(url : string) : void,
	navigateTo(url : string) : void,
	getCurrentPage() : string,
}

const app: App = {
    web: webConfig,
    GUID_EMPTY: "00000000-0000-0000-0000-000000000000", // "0000000000000000000000",
    baseDepart: "70ca9a3a-a116-41fd-93f7-a5d421aec030", // 厦门大学的ID
    delay(ms) {
        return new Promise((resolve) => {
            setTimeout(resolve, ms);
        });
    },
    toText: () => "",
    setTitle (title) {
        document.title = title ? `${title} - ${webConfig.name}` : webConfig.name;
    },
    sha1,
    sha256,
    accessToken,
    currentUser,
    succeed: currentUser.succeed,
    withResolvers,
    isDevelopment () {
        return webConfig.debug;
    },
	navigateTo (url) {
		uni.navigateTo({
			url,
		})
	},
	redirectTo (url) {
		uni.redirectTo({
			url,
		})
	},
	getCurrentPage() {
		let pages = getCurrentPages();
		return pages.length ? (pages[pages.length - 1]).route : "";
	}
};

export default app;